<?php

namespace app\api\controller;

use app\api\model\UserUploadAttachment;
use app\common\controller\Api;
use think\Request;

/**
 * 文件接口
 */
class Attachment extends Api
{
    protected $noNeedRight = ['*'];

    /**
     * 用户上传文件列表
     */
    public function list()
    {
        // 文件类型 1打印文件 2字体文件
        $type = $this->request->get('type',0);
        // 页码
        $page = $this->request->get('page',1);
        // 每页数量
        $pageNum = $this->request->get('page_num',10);

        if(empty($type)){
            $this->error('请选择文件类型');
        }
        $user = $this->auth->getUser();
        $where =[
            'user_id' => $user->id,
            'type' => $type,
        ];

        $list = model('UserUploadAttachment')
            ->where($where)
            ->order('id', 'desc')
            ->limit(($page-1)*$pageNum,$pageNum)->field('id,
                user_id,
                CONCAT("'.request()->domain().'",url) as url,
                filename,
                FROM_UNIXTIME(createtime,"%Y.%m.%d %H:%m") as createtime,
                author')->select();
        $this->success('文件列表',$list);
    }

    /**
     * 添加字体文件
    */
    public function add()
    {
        if(!Request::instance()->isPost()){
            $this->error('请求类型错误');
        }
        $userInfo = $this->auth->getUserinfo();
        $name = $this->request->post('name');
        if(empty($name)){
            $this->error('请输入字体名称');
        }
        $author = $this->request->post('author');
        if(empty($author)){
            $this->error('请输入字体作者');
        }
        $url = $this->request->post()['url'];
        if(empty($url)){
            $this->error('请上传字体文件');
        }
        $userAttachment = new UserUploadAttachment();
        $userAttachment->user_id = $userInfo['id'];
        $userAttachment->filename = $name;
        $userAttachment->author = $author;
        $userAttachment->url = $url[0];
        $userAttachment->type = 2;
        $userAttachment->save();
        $result = $this->addPath($url,$userAttachment->id);
        $this->success('添加成功',$result);
    }

    /**
     * 字体详情
    */
    public function detail()
    {
        $id = $this->request->get('id');
        if(empty($id)){
            $this->error('请选择字体');
        }
        $attachmentInfo = model('UserUploadAttachment')->where(
            'id',
            '=',
            $id)->field('id,
                type,
                user_id,
                filename,
                FROM_UNIXTIME(createtime,"%Y-%m-%d %H:%m:%s") as createtime,
                author')->find();
        if($attachmentInfo->type != 2){
            $this->error('数据错误');
        }
        if(!$attachmentInfo){
            $this->error('参数错误，未找到该数据');
        }
        $pathList = model('UserUploadAttachmentPath')->where(
            'attachment_id',
            '=',
            $attachmentInfo->id
        )->field('id,
                attachment_id,
                CONCAT("'.request()->domain().'",url) as url,
                is_entry,
                FROM_UNIXTIME(createtime,"%Y-%m-%d %H:%m:%s") as createtime
                ')->select();
        $attachmentInfo->path_list = $pathList;
        $this->success('字体详情',$attachmentInfo);
    }

    /**
     * 更新字体文件
    */
    public function update()
    {
        if(!Request::instance()->isPost()){
            $this->error('请求类型错误');
        }
        $id = $this->request->post('id');
        if(empty($id)){
            $this->error('请选择要删除的数据');
        }
        $attachmentInfo = model('UserUploadAttachment')->where('id','=',$id)->find();
        if(!$attachmentInfo){
            $this->error('参数错误，未找到该数据');
        }
        model('UserUploadAttachmentPath')->where(
            'attachment_id',
            '=',
            $attachmentInfo->id
        )->delete();
        $url = $this->request->post()['url'];
        $attachmentInfo->url =  $url[0];
        $attachmentInfo->save();
        $result = $this->addPath($url,$attachmentInfo->id);
        $this->success('更新成功',$result);
    }


    /**
     * 添加字体附表
    */
    private function addPath($urlList,$attachmentId)
    {
        $pathList = [];
        foreach ($urlList as $item){
            $pathList[] = [
                'attachment_id' => $attachmentId,
                'url' => str_replace(request()->domain(),'',$item),
                'is_entry' => 0,
                'createtime' => time(),
                'updatetime' => time(),
            ];
        }
        return model('UserUploadAttachmentPath')->insertAll($pathList);
    }

    /**
     * 录入字体
    */
    public function entry()
    {
        if(!Request::instance()->isPost()){
            $this->error('请求类型错误');
        }
        $id = $this->request->post()['id'];
        if(empty($id)){
            $this->error('请选择要录入的字体');
        }
        $result = model('UserUploadAttachmentPath')->whereIn('id',$id)->update([
            'is_entry' => 1
        ]);
        $this->success('录入成功',$result);

    }

    /**
     * 删除文件或字体
    */
    public function delete()
    {
        if(!Request::instance()->isPost()){
            $this->error('请求类型错误');
        }
        $id = $this->request->post('id');
        if(empty($id)){
            $this->error('请选择要删除的数据');
        }
        $attachmentInfo = model('UserUploadAttachment')->where('id','=',$id)->find();
        if(!$attachmentInfo){
            $this->error('参数错误，未找到该数据');
        }
        $userInfo = $this->auth->getUserinfo();
        if($attachmentInfo->user_id!=$userInfo['id']){
            $this->error('您没有权限删除该数据');
        }
        $attachmentInfo->delete();
        if($attachmentInfo->type == 2){
            model('UserUploadAttachmentPath')->where(
                'attachment_id',
                '=',
                $attachmentInfo->id
            )->delete();
        }
        $this->success('删除成功');
    }
}
